//
//  KSKARizumuController.m
//  DashboardHD
//
//  Created by 楊亜玲 on 17/3/27.
//  Copyright © 2017年 Totyu. All rights reserved.
//

#import "KSKARizumuController.h"
#import "KSKARizumuTool.h"
#import "KSKABarChart.h"

@interface KSKARizumuCell : UICollectionViewCell

/** 显示日期 */
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
/** 显示图表 */
@property (weak, nonatomic) IBOutlet UIView *chartView;

@end

@implementation KSKARizumuCell

@end

@interface KSKARizumuController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

/** 一周的数据数组(包含7个字典数组) */
@property (nonatomic, strong) NSArray *dataArray;

/** 一周的日期数组(用于label显示) */
@property (nonatomic, strong) NSArray *dateArray;

@end

@implementation KSKARizumuController

static NSString * const reuseIdentifier = @"rizumuCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_actionid.length){
        [self loadDataWithBasedate:self.mondayStr actionid:self.actionid];
    }else{
        [self loadDataWithBasedate:self.mondayStr actionid:nil];
    }
    
}


/**
 网络请求数据

 @param dateStr 基准日
 @param actionid 活动id
 */
-(void)loadDataWithBasedate:(NSString *)dateStr actionid:(NSString *)actionid{
    
    // 请求参数
    KSKARizumuParam *param = [[KSKARizumuParam alloc]init];
    param.userid0 = @"0002";
    param.basedate = dateStr;
    NSMutableArray *resultArr = [NSMutableArray array];
    NSMutableArray *dateArr = [NSMutableArray array];
    
    [MBProgressHUD showHUDAddedTo:self.collectionView
                         animated:YES];
    // 发送请求
    [KSKARizumuTool rizumuWeekListWithParam:param success:^(NSArray *array) {
        if (array.count > 0) {
            for (int i = 0; i < array.count; i++) {
                NSDictionary *tmpdict = [array objectAtIndex:i];
                NSString *key = [[tmpdict allKeys]firstObject];
                NSArray *tmparr = [tmpdict objectForKey:key][1];// 1天的数据源（包含6个字典）
                // 提取数据
                if (actionid.length) {
                    for (NSDictionary *dict in tmparr) {
                        if ([[dict valueForKey:@"actionid"] isEqualToString:self.actionid]) {
                            NSArray *tmparr2 = [NSArray arrayWithObject:dict];
                            [resultArr addObject:tmparr2]; //7天的数据源（包含7个字典数组）
                        }
                    }
                }else{
                    [resultArr addObject:tmparr];
                }
                // 日期
                [dateArr addObject:key];
            }
            self.dataArray = resultArr;
            self.dateArray = dateArr;
            
            // 刷新图表
            [self.collectionView reloadData];
            
        }else{
#warning todo
            KSKLog(@"网络返回空数组");
            
        }
        [MBProgressHUD hideHUDForView:self.collectionView animated:YES];
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUDForView:self.collectionView animated:YES];
#warning todo
    }];
    
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //return 0;
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KSKARizumuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell setNeedsLayout];
    [cell layoutIfNeeded];
    NSArray *cellData = [self.dataArray objectAtIndex:indexPath.item];
    // 显示日期
    cell.dateLabel.text = [_dateArray objectAtIndex:indexPath.item];
    // 添加图表view
    KSKABarChart *chart = [[KSKABarChart alloc]initWithFrame:cell.chartView.bounds dataSource:cellData];
    cell.chartView.backgroundColor = [UIColor yellowColor];
    [cell.chartView addSubview:chart];

   
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.collectionView.width,self.collectionView.height / 7);
}


@end
